# client.py (运行在Linux上)
import socket
import subprocess

def start_client(server_host='10.1.30.235', server_port=65432):
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as client_socket:
        client_socket.connect((server_host, server_port))
        print(f"Connected to server at {server_host}:{server_port}")

        while True:
            data = client_socket.recv(1024).decode('utf-8')
            if not data:
                break
            print(f"Received command: {data}")


            # 打印接收到的命令
            print(f"Received command from server: {data}")


            # 执行Shell命令
            try:
                output = subprocess.check_output(data, shell=True, stderr=subprocess.STDOUT)
                response = output.decode('utf-8')
                print(f"Command executed successfully:\n{response}")
            except subprocess.CalledProcessError as e:
                response = e.output.decode('utf-8')
                print(f"Command execution failed:\n{response}")

            client_socket.sendall(response.encode('utf-8'))

if __name__ == "__main__":
    start_client()
